package fischl.web;

import static fi.iki.elonen.NanoHTTPD.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;
import fischl.Global;
import fischl.Platform;
import fischl.model.VideoModel;

public class IndexHandler implements RouteHandler {
  @Override
  public NanoHTTPD.Response handle(NanoHTTPD.IHTTPSession session) {
    final String uri = session.getUri();
    if (!uri.equals("/index") && !uri.equals("/")) {
      return null;
    }

    List<VideoModel> videos = Platform.current().getSnapshot();
    Map<String, Object> model = new HashMap<>();
    model.put("timeline", Global.gson().toJson(videos));
    model.put("sendEnabled", String.valueOf(false));
    String html = TemplateEngine.render("timeline.ftlh", model);
    return newFixedLengthResponse(Response.Status.OK, "text/html", html);
  }
}
